package com.init.engineering.controller;

import com.init.engineering.pages.PageResult;
import com.init.engineering.pages.QueryPageBean;
import com.init.engineering.pages.Result;
import com.init.engineering.pojo.Developer;
import com.init.engineering.pojo.Project;
import com.init.engineering.service.DeveloperService;
import com.init.engineering.service.ProjectService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("/developer")
public class DeveloperController {
    @Autowired
    private DeveloperService developerService;

    @Autowired
    private ProjectService projectService;

    /**
     * 查询所有开发商
     * @return
     */
    @RequestMapping("/findDevelopers")
    public Result findDevelopers(){
        List<Developer> developers = developerService.findDevelopers();
        Result result = new Result();
        if(developers != null){
            result.setData(developers);
            result.setFlag(true);
        }else{
            result.setFlag(false);
        }
        return result;
    }

    /**
     * 通过开发商ID查找开发商
     * @return
     */
    @RequestMapping("/findIDs")
    public Result findDevelopersDid(){
        List<Developer> developers = developerService.findDevelopers();
        List<String> dids = new ArrayList<String>();
        Result result = new Result();
        if(developers != null){
            for(int i = 0;i < developers.size();i++){
                dids.add(developers.get(i).getDid());
            }
            result.setData(dids);
            result.setFlag(true);
        }else{
            result.setFlag(false);
        }
        return result;
    }


    /**
     * 查询所有开发商ID
     * @return
     */
    @RequestMapping("/findNames")
    public Result findDevelopersDname(){
        List<Developer> developers = developerService.findDevelopers();
        List<String> dnames = new ArrayList<String>();
        Result result = new Result();
        if(developers != null){
            for(int i = 0;i < developers.size();i++){
                dnames.add(developers.get(i).getDname());
            }
            result.setData(dnames);
            result.setFlag(true);
        }else{
            result.setFlag(false);
        }
        return result;
    }



    /**
     * 分页查询所有开发商
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/all")
    public PageResult findDevelopersByPages(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = developerService.findDevelopersByPage(queryPageBean);
        return pageResult;
    }

    /**
     * 删除开发商，将status状态设置为0，同时修改工程内的did属性
     * @param did
     * @return
     */
    @GetMapping("/delete/{did}")
    @ApiOperation(value = "删除开发商",notes = "删除接口",httpMethod = "get")
    public Result deleteDeveloper(@PathVariable("did") String did){
        int row = developerService.deleteDeveloper(did);
        Result result = new Result();
        if(row > 0){
            result.setMessage("开发商删除成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("开发商删除失败，请重试");
        }
        //将开发商对应的工程内的did设置为空
        List<Project> projects = projectService.findProjectsByDid(did);
        //判断开发商是否存在对应工程
        if(projects != null){
            for(int i = 0;i < projects.size();i++){
                Project project = projects.get(i);
                project.setDid("");
                projectService.updateProject(project);
            }
        }
        return result;
    }

    /**
     * 新增开发商
     * @RequestBody 来接收前端传递给后端的json字符串中的数据,将该json数据对象转为java对象
     * @param developer
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增开发商",notes = "添加接口",httpMethod = "post")
    public Result addProject(@RequestBody Developer developer){
        System.out.println(developer.toString());
        //获取系统更新时间
        developer.setUpdatetime(new Date());
        int row = developerService.saveDeveloper(developer);
        Result result = new Result();
        if(row > 0){
            result.setMessage("开发商添加成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("开发商添加失败，请重试");
        }
        return result;
    }


    @RequestMapping("/findByID/{did}")
    public Result findDeveloperByDid(@PathVariable("did") String did){
        Developer developer = developerService.findDeveloperByDid(did);
        Result result = new Result();
        if (developer != null){
            result.setFlag(true);
            result.setData(developer);
        }else{
            result.setFlag(false);
            result.setMessage("数据加载中");
        }
        return result;
    }

    /**
     * 进行开发商的更新
     * @param developer
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新开发商",notes = "更新接口",httpMethod = "post")
    public Result updateDeveloper(@RequestBody Developer developer){
        developer.setUpdatetime(new Date());
        int row = developerService.UpdateDeveloper(developer);
        Result result = new Result();
        if(row > 0){
            result.setMessage("开发商更新成功");
            result.setFlag(true);
        }else{
            result.setFlag(false);
            result.setMessage("开发商更新失败，请重试");
        }
        return result;
    }
}
